package com.ma.schoolproject.utils;

import java.util.regex.Pattern;

/**
 * 手机号码校验工具类
 */
public class PhoneValidator {
    
    // 手机号码正则表达式：11位数字，以1开头，第二位为3-9
    private static final String PHONE_PATTERN = "^1[3-9]\\d{9}$";
    private static final Pattern PATTERN = Pattern.compile(PHONE_PATTERN);
    
    /**
     * 校验手机号码格式
     * @param phone 手机号码
     * @return 是否合法
     */
    public static boolean isValidPhone(String phone) {
        if (phone == null || phone.isEmpty()) {
            return false;
        }
        return PATTERN.matcher(phone).matches();
    }
    
    /**
     * 校验手机号码格式并返回详细信息
     * @param phone 手机号码
     * @return 校验结果信息
     */
    public static PhoneValidationResult validate(String phone) {
        if (phone == null) {
            return new PhoneValidationResult(false, "手机号码不能为空");
        }
        
        String trimmedPhone = phone.trim();
        
        if (trimmedPhone.isEmpty()) {
            return new PhoneValidationResult(false, "手机号码不能为空");
        }
        
        if (trimmedPhone.length() != 11) {
            return new PhoneValidationResult(false, "手机号码必须为11位");
        }
        
        if (!trimmedPhone.startsWith("1")) {
            return new PhoneValidationResult(false, "手机号码必须以1开头");
        }
        
        if (!PATTERN.matcher(trimmedPhone).matches()) {
            return new PhoneValidationResult(false, "手机号码格式不正确");
        }
        
        return new PhoneValidationResult(true, "手机号码格式正确");
    }
    
    /**
     * 手机号验证结果类
     */
    public static class PhoneValidationResult {
        private final boolean valid;
        private final String message;
        
        public PhoneValidationResult(boolean valid, String message) {
            this.valid = valid;
            this.message = message;
        }
        
        public boolean isValid() {
            return valid;
        }
        
        public String getMessage() {
            return message;
        }
    }
}
